package Alert;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import javax.print.attribute.AttributeSet;
import javax.swing.ComboBoxEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;
import mysql.connect;
import STOCK.STOCK;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddAlert.java
 *
 * Created on Sep 10, 2011, 7:27:53 PM
 */
/**
 *
 * @author Anatomy
 */

 

public class AddAlert extends javax.swing.JDialog {
    private int count;
    public String id;
    private DefaultTableModel model1;
    /** Creates new form AddAlert */
    public AddAlert(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screenSize = tk.getScreenSize();
    int screenHeight = screenSize.height;
    int screenWidth = screenSize.width;
   // setSize(screenWidth / 2, screenHeight / 2);
    setLocation(screenWidth / 4, screenHeight / 4);
      setTitle("Edit Alert");
   
        // add data
        // txtAddSysbol.add(new JScrollPane(list)); 
  
  

    }

    public AddAlert() {
        //throw new UnsupportedOperationException("Not yet implemented");
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    
    
  
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lbFilePath = new javax.swing.JLabel();
        btBrowser = new javax.swing.JButton();
        btaddalert = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtlow = new javax.swing.JTextField();
        txtup = new javax.swing.JTextField();
        cbsoundstate = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Alert");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("SoundType :");

        lbFilePath.setText("..");
        lbFilePath.setEnabled(false);

        btBrowser.setText("...");
        btBrowser.setToolTipText("");
        btBrowser.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btBrowser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBrowserActionPerformed(evt);
            }
        });

        btaddalert.setText("Edit Sound");
        btaddalert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btaddalertActionPerformed(evt);
            }
        });

        jLabel2.setText("Low : ");

        jLabel3.setText("Up :");

        cbsoundstate.setText("เสียงเตือน");
        cbsoundstate.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbsoundstateItemStateChanged(evt);
            }
        });
        cbsoundstate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbsoundstateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtlow, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                            .addComponent(txtup))
                        .addGap(80, 80, 80)
                        .addComponent(btaddalert))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btBrowser, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lbFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(cbsoundstate))
                        .addGap(0, 55, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtlow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btaddalert))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btBrowser)
                    .addComponent(lbFilePath, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbsoundstate)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
String string;
private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
try {String Path="";
            connect dbcon=new connect(); 
                       String    sqlsec="SELECT * FROM `stock_ alert` where stockname='"+getId()+"'";
                       System.out.println(sqlsec);
                    //  String    sqlsec="SELECT * FROM `stock_ alert` where id='"+getId()+"'";
                     
                      dbcon.rs= dbcon.stmt.executeQuery(sqlsec);
                      dbcon.rs.next();
             string = dbcon.rs.getString("soundtype");
                      lbFilePath.setText(dbcon.rs.getString("soundtype"));
                      txtlow.setText(dbcon.rs.getString("value"));
                      txtup.setText(dbcon.rs.getString("value1"));
                      String chkst=dbcon.rs.getString("state");
                      if(chkst.equals("1")){
                           cbsoundstate.setSelected(true);
                      }else{
                          cbsoundstate.setSelected(false);
                      }
                     
                      dbcon.CloseDB();
                       
                       
            
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        
        
        
}//GEN-LAST:event_formWindowOpened
int Keypress=0;
private void btBrowserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBrowserActionPerformed
Fc Fc=new Fc(null,true);
Fc.show();// TODO add your handling code here:
lbFilePath.setText(Fc.FilePath);
if(!lbFilePath.getText().equals("")){
    btaddalert.setEnabled(true);
}
}//GEN-LAST:event_btBrowserActionPerformed

private void btaddalertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btaddalertActionPerformed
        try {String Path="";
            connect dbcon=new connect(); String PathCon ; 
                        if(lbFilePath.getText().equals("..")||lbFilePath.getText().equals("")){
                             PathCon="\\lib\\sound\\alert.mp3";
                         }else{
                           Path=lbFilePath.getText(); 
                       PathCon = Path.substring(6, Path.length());
                        }
                        String update;
                        if(string.equals(lbFilePath.getText())){
                        update = "UPDATE `stock_data`.`stock_ alert` SET `value` = '"+txtlow.getText()+"',"+
                             
                       
                        "`value1` = '"+txtup.getText()+"',"+
                        "`state` = '"+chkstate+"' WHERE `stock_ alert`.`stockname` ='"+getId()+"';";
                        }else{
                        update= "UPDATE `stock_data`.`stock_ alert` SET `value` = '"+txtlow.getText()+"',"+
                             
                        "`soundtype` = '"+PathCon+"',"+
                        "`value1` = '"+txtup.getText()+"',"+
                        "`state` = '"+chkstate+"' WHERE `stock_ alert`.`stockname` ='"+getId()+"';";
                        }

                     
                      dbcon.stmt.executeUpdate(update);
                      dbcon.CloseDB();
                       
                       
              this.dispose();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        
        
        
        
        
        
        
    GetAlert();
   

        
        
        
        
        
        
        
}//GEN-LAST:event_btaddalertActionPerformed

    private void cbsoundstateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbsoundstateActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbsoundstateActionPerformed
int chkstate;
    private void cbsoundstateItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbsoundstateItemStateChanged
if(cbsoundstate.isSelected()){
          chkstate=1;                  
                        } else{  chkstate=0;   
}// TODO add your handling code here:
    }//GEN-LAST:event_cbsoundstateItemStateChanged
String Col1[]={"Name","Low","Up","Sound"};
public void GetAlert(){
     try {
            DefaultTableModel model=new DefaultTableModel(Col1,0);
        


            
             connect dbcon1=new connect();
           
            String    sqlsec1="SELECT * FROM `stock_ alert`";
            dbcon1.rs= dbcon1.stmt.executeQuery(sqlsec1);
             while(dbcon1.rs.next()){
                 String x[]={dbcon1.rs.getString(2),dbcon1.rs.getString(3),dbcon1.rs.getString(5),dbcon1.rs.getString(4)}; 
       
                // System.err.println(dbcon.rs.getString(2)+dbcon.rs.getString(3)+dbcon.rs.getString(4));
                 model.addRow(x);
             }
          
            model1=model;
            STOCK.TbAlert.setModel(model);
          dbcon1.CloseDB();
          
               
             
        } catch (SQLException ex) {
            ex.printStackTrace();
            
        }
        
        STOCK.TbAlert.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
       
        STOCK.TbAlert.getColumnModel().getColumn(0).setResizable(false);
        STOCK.TbAlert.getColumnModel().getColumn(0).setPreferredWidth(100);
        STOCK.TbAlert.getColumnModel().getColumn(1).setResizable(false);
        STOCK.TbAlert.getColumnModel().getColumn(1).setPreferredWidth(80);
        STOCK.TbAlert.getColumnModel().getColumn(2).setResizable(false);
        STOCK.TbAlert.getColumnModel().getColumn(2).setPreferredWidth(80);
        STOCK.TbAlert.getColumnModel().getColumn(3).setResizable(true);
        STOCK.TbAlert.getColumnModel().getColumn(3).setPreferredWidth(700);   
      
          
}
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
       
             Properties props = new Properties(); 
             props.put("logoString", "SEDSS");
             com.jtattoo.plaf.acryl.AcrylLookAndFeel.setCurrentTheme(props);
                
        
            UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");
            
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                AddAlert dialog = new AddAlert(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.pack();
                dialog.setVisible(true);
              
         
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBrowser;
    private javax.swing.JButton btaddalert;
    private javax.swing.JCheckBox cbsoundstate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel lbFilePath;
    private javax.swing.JTextField txtlow;
    private javax.swing.JTextField txtup;
    // End of variables declaration//GEN-END:variables
}

